package bTree.LeetCode;

import java.util.Arrays;

public class 高度互不相同的最大塔高和 {
    public long maximumTotalSum(int[] maximumHeight) {
        int length = maximumHeight.length;
        Arrays.sort(maximumHeight);
        long num = maximumHeight[length - 1];
        for (int i = length - 2; i >= 0; i--) {
            //最核心的一个
            maximumHeight[i] = Math.min(maximumHeight[i + 1] - 1, maximumHeight[i]);
            if (maximumHeight[i] <= 0) {
                return -1;
            }
            num += maximumHeight[i];
        }
        return num;
    }
}